/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: vos_os_linux.c
 *
 * Purpose: virtual os API implement in linux system
 *
 * Developer:
 *   wen.gu , 2015-06-3
 *
 * TODO: 
 *
 ***************************************************************************/

//inner header file
//#include "sys_config.h"

#if defined(G_OS_LINUX)
//#include "vos.h"
//#include "vos_log.h"



/**
*
*@brief  vos default log write API
*
*@param  opaque [in] the parameter of log write callback function.
*@param  level  [in] log priority level.
*@param  prefix [in] log target name.
*@param  fmt    [in] The format string (printf-compatible) that specifies how
*                   subsequent arguments are converted to output.
*@param  vl     [in] The arguments referenced by the format string.
*
*@return None.
*
*@see
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>


#define LOG_TEMP_BUF_LEN  1024 * 2

static pthread_mutex_t gLogMutex = PTHREAD_MUTEX_INITIALIZER;



static GS08 gLogTempBuf[LOG_TEMP_BUF_LEN + 1] = { 0 };

/**
*
*@brief  Send the specified message to the log if the level is less than or
*        equal to the current log level. all logging messages are sent to 
*        the callback function of 'cb' in iLogWrite Param.
*
*@param  opaque [in] the parameter of log write callback function.
*@param  level  [in] log priority level.
*@param  tag    [in] vos log tag
*@param  func   [in] the name of function
*@param  line   [in] line number
*@param  fmt    [in] The format string (printf-compatible) that specifies how
*                   subsequent arguments are converted to output.
*@param  vl     [in] The arguments referenced by the format string.
*
*@return None.
*
*@see   
*/
void vosLogWrite(void* opaque, GS32 level, const char* tag, const char* func, int line, const char* fmt, va_list vl)

{
    if (level > logGetLevel())
    {
        return;
    }
    {
        GS08* tempBuf = gLogTempBuf;
        GS32 retSize = 0;

        pthread_mutex_lock(&gLogMutex);
		//[tag][func, line][levelname] xxxx
		retSize = snprintf(tempBuf, LOG_TEMP_BUF_LEN,"[%s][%s,%d]%s%s",tag, func, line,logGetLevelName(level), fmt);
		assert(retSize < LOG_TEMP_BUF_LEN);

        vfprintf(stderr, tempBuf, vl);

        pthread_mutex_unlock(&gLogMutex);
    }
}

#endif
